import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy, VerifyCallback } from 'passport-wechat';
import { AuthService } from './auth.service';

@Injectable()
export class WechatStrategy extends PassportStrategy(Strategy, 'wechat') {
  constructor(private authService: AuthService) {
    super({
      appID: process.env.WECHAT_APP_ID || 'your-wechat-app-id',
      appSecret: process.env.WECHAT_APP_SECRET || 'your-wechat-app-secret',
      callbackURL: process.env.APP_URL ? `${process.env.APP_URL}/auth/wechat/callback` : 'http://localhost:3000/auth/wechat/callback',
      scope: ['snsapi_userinfo'],
    });
  }

  async validate(
    accessToken: string,
    refreshToken: string,
    profile: any,
    done: VerifyCallback,
  ): Promise<any> {
    const { id, nickname, sex, headimgurl } = profile._json;

    const user = {
      wechatId: id,
      nickname: nickname,
      gender: sex,
      avatar: headimgurl,
      accessToken,
      refreshToken,
    };

    done(null, user);
  }
}